package com.superdry.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.drysurper.R;
import com.superdry.json.Clothes;
import com.superdry.utils.SPHelper;
import com.superdry.view.MyGridView;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter implements OnItemClickListener {
	public static final int ItemHeight = 48;
	public static final int PaddingLeft = 36;
	private int myPaddingLeft = 0;
	private MyGridView toolbarGrid;
	TextView total_count;
	TextView total_money;
	private List<TreeNode> treeNodes = new ArrayList<TreeNode>();
	private Context parentContext;
	private LayoutInflater layoutInflater;

	static public class TreeNode {
		public String parent;
		public Clothes childs;
	}

	public ExpandableListViewAdapter(Context view, int myPaddingLeft, TextView total_count, TextView total_money) {
		parentContext = view;
		this.myPaddingLeft = myPaddingLeft;
		this.total_count = total_count;
		this.total_money = total_money;
	}

	public List<TreeNode> GetTreeNode() {
		return treeNodes;
	}

	public void UpdateTreeNode(List<TreeNode> nodes) {
		treeNodes = nodes;
	}

	public void RemoveAll() {
		treeNodes.clear();
	}

	public Object getChild(int groupPosition, int childPosition) {
		// return treeNodes.get(groupPosition).childs.get(childPosition);
		return "";
	}

	public int getChildrenCount(int groupPosition) {
		// return treeNodes.get(groupPosition).childs.size();
		return 1;
	}

	static public TextView getTextView(Context context) {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);

		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		return textView;
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		GridViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new GridViewHolder();
			layoutInflater = (LayoutInflater) parentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.view, null);
			viewHolder.mygridview = (MyGridView) convertView.findViewById(R.id.GridView_toolbar);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (GridViewHolder) convertView.getTag();
		}
		viewHolder.mygridview.setNumColumns(9);
		viewHolder.mygridview.setGravity(Gravity.CENTER);
		viewHolder.mygridview.setHorizontalSpacing(10);
		viewHolder.mygridview.setAdapter(getAdapter(groupPosition));
		return convertView;

	}

	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		final String type = treeNodes.get(groupPosition).parent;
		final Clothes child = treeNodes.get(groupPosition).childs;
		final String season = child.getSeasonType();
		if (convertView == null) {
			viewHolder = new ViewHolder();
			layoutInflater = (LayoutInflater) parentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.list_view, null);
			convertView.setBackgroundColor(Color.WHITE);
			viewHolder.expandableListView_arrow = (ImageView) convertView.findViewById(R.id.expandableListView_arrow);
			viewHolder.cloth_count_add = (ImageView) convertView.findViewById(R.id.cloth_count_add);
			viewHolder.cloth_count_del = (ImageView) convertView.findViewById(R.id.cloth_count_del);
			viewHolder.cloth_count = (TextView) convertView.findViewById(R.id.cloth_count);
			viewHolder.cloth_type = (TextView) convertView.findViewById(R.id.cloth_type);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (isExpanded) {
			viewHolder.expandableListView_arrow.setBackgroundResource(R.drawable.arrow_up);
		} else {
			viewHolder.expandableListView_arrow.setBackgroundResource(R.drawable.arrow_down);
		}
		viewHolder.cloth_count.setText(SPHelper.getInt(parentContext, type) + "");
		viewHolder.cloth_type.setText(type + "元区");
		viewHolder.cloth_count_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				SPHelper.putInt(parentContext, type, SPHelper.getInt(parentContext, type) + 1);
				refreshTotal();
				notifyDataSetChanged();
			}
		});
		viewHolder.cloth_count_del.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (SPHelper.getInt(parentContext, type) <= 0) {
					return;
				} else {
					SPHelper.putInt(parentContext, type, SPHelper.getInt(parentContext, type) - 1);
					refreshTotal();
					notifyDataSetChanged();
				}
			}
		});

		return convertView;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	public Object getGroup(int groupPosition) {
		return treeNodes.get(groupPosition).parent;
	}

	public int getGroupCount() {
		return treeNodes.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	private ArrayAdapter<String> getAdapter(int position) {
		Clothes cloth = treeNodes.get(position).childs;
		List<String> item = new ArrayList<String>();
		for (String str : cloth.getDetails().split(",")) {
			item.add(str);
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(parentContext, R.layout.item_menu, item);
		return adapter;
	}

	/**
	 * 刷新显示的总数据
	 */
	private void refreshTotal() {
		int all_count = 0;
		int all_money = 0;
		for (int i = 0; i < treeNodes.size(); i++) {
			int price = (int) treeNodes.get(i).childs.getPrice();
			int count = SPHelper.getInt(parentContext, price + "");
			all_count = all_count + count;
			all_money = all_money + count * price;
		}
		total_count.setText(all_count + "");
		total_money.setText(all_money + "");
		SPHelper.putInt(parentContext, SPHelper.TOATAL_MONEY, all_money);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	class ViewHolder {
		private ImageView cloth_count_del;
		private ImageView cloth_count_add;
		private ImageView expandableListView_arrow;
		private TextView cloth_count;
		private TextView cloth_type;
	}

	class GridViewHolder {
		MyGridView mygridview;
	}

}